package testview.zhen.com.myapplication;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import com.lzf.easyfloat.EasyFloat;

/**
 * Create by ldr
 * on 2019/7/29 15:02.
 * 判断app是否在前台的办法 监听app的
 */
public class App extends Application implements Application.ActivityLifecycleCallbacks{

    private static final String TAG = "App";
    int activityCount = 0;
    public static boolean isForeground = false;

    @Override
    public void onCreate() {
        super.onCreate();
        //全局管理Activity生命周期
        registerActivityLifecycleCallbacks(this);
//        EasyFloat.init(this, true);
    }

    /**
     * 判断应用是否在前台 使用计数的方法
     */
    private void isForeground(){
        if (activityCount > 0){
            isForeground = true;
        }else{
            isForeground = false;
        }
        Log.i(TAG, "isForeground: "+activityCount);
    }
    ///////////////////-------------- 以下的方法是ActivityLifecycleCallbacks的实现方法 ----------------------////////////////////////
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        Log.i(TAG, "onActivityCreated: "+activityCount);
        isForeground();
    }

    @Override
    public void onActivityStarted(Activity activity) {
        activityCount++;
        Log.i(TAG, "onActivityStarted: "+activityCount);
        isForeground();
    }

    @Override
    public void onActivityResumed(Activity activity) {
        Log.i(TAG, "onActivityResumed: "+activityCount);
        isForeground();
    }

    @Override
    public void onActivityPaused(Activity activity) {
        Log.i(TAG, "onActivityPaused: "+activityCount);
        isForeground();
    }

    @Override
    public void onActivityStopped(Activity activity) {
        activityCount--;
        Log.i(TAG, "onActivityStopped: "+activityCount);
        isForeground();
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        Log.i(TAG, "onActivitySaveInstanceState: "+activityCount);
        isForeground();
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        Log.i(TAG, "onActivityDestroyed: "+activityCount);
        isForeground();
    }
}
